﻿/*********************************************************************************
**                                                                              **
**  Copyright (C) 2024 LiLong                                                   **
**  This file is part of GnssLogs.                                              **
**                                                                              **
**  GnssLogs is free software: you can redistribute it and/or modify            **
**  it under the terms of the GNU Lesser General Public License as published by **
**  the Free Software Foundation, either version 3 of the License, or           **
**  (at your option) any later version.                                         **
**                                                                              **
**  GnssLogs is distributed in the hope that it will be useful,                 **
**  but WITHOUT ANY WARRANTY; without even the implied warranty of              **
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               **
**  GNU Lesser General Public License for more details.                         **
**                                                                              **
**  You should have received a copy of the GNU Lesser General Public License    **
**  along with GnssLogs.  If not, see <https://www.gnu.org/licenses/>.          **
**********************************************************************************/
#pragma once

#include "../HDT.h"
#include "Commons.h"

namespace GnssLogs
{
namespace HDTDecoder
{
using namespace tao::pegtl;
using LogHeader = NMEA_TALKER_IDENTIFIER("HDT");
struct Heading : Floating
{
};
struct DegreesTree : one<'T'>
{
};

using Expression = seq<LogHeader, Split, Heading, Split, DegreesTree, one<'*'>, CheckSum, Tail>;

template<typename T>
struct Action
{
};

template<>
struct Action<LogHeader>
{
    template<typename ParseInput>
    static bool apply(ParseInput& in, HDT::Data& data)
    {
        data.header = std::string(in.begin(), in.size());
        return true;
    }
};

template<>
struct Action<Heading> : DoubleHelper<Action<Heading>>
{
    static void setValue(HDT::Data& data, double value) { data.heading = value; }
};

template<>
struct Action<DegreesTree>
{
    template<typename ParseInput>
    static bool apply(ParseInput& in, HDT::Data& data)
    {
        data.degreesTrue = std::string(in.begin(), in.size());
        return true;
    }
};

template<>
struct Action<CheckSum> : CheckSumHelper<Action<CheckSum>>
{
    static void setValue(HDT::Data& data, uint8_t value) { data.checkSum = value; }
};
} // namespace HDTDecoder
} // namespace GnssLogs